﻿Imports Aricie.DNN.Modules.LuceneSearch.UI
Imports Aricie.ComponentModel
Imports Lucene.Net.Search
Imports Lucene.Net.Index
Imports Aricie.DNN.Modules.LuceneSearch.Business

Namespace Aricie.DNN.Modules.LuceneSearch.UI

    Partial Public Class AlphabetFilter
        Inherits FieldFilterBase

        Public Event Click(ByVal lettre As String)

        Private _Enabled As Boolean = True
        Public Overrides Property Enabled() As Boolean
            Get
                Return _Enabled
            End Get
            Set(ByVal value As Boolean)
                _Enabled = value
            End Set
        End Property

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            BindAlphabet()
        End Sub

        Private Sub BindAlphabet()
            Dim alphabet As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
            Dim i As Integer = 0
            For Each lettre As String In alphabet
                Dim link As New LinkButton()
                Dim spacer As New HtmlGenericControl("span")

                If i = 13 Then
                    If Me.DisplayOnTwoLines Then
                        spacer.InnerHtml = " "
                    Else
                        spacer.InnerHtml = "&nbsp;"
                    End If
                Else
                    spacer.InnerHtml = "&nbsp;"
                End If

                link.Text = lettre
                link.CommandArgument = lettre
                link.CssClass = "Normal"
                link.Enabled = Enabled
                AddHandler link.Click, AddressOf Lettre_Click

                pnAlpha.Controls.Add(link)
                pnAlpha.Controls.Add(spacer)
                i = i + 1
            Next
        End Sub

        Public Overrides Function GetFilter() As LuceneFilterQuery
            Dim PQ As New PrefixQuery(New Term(FieldName, Me.FieldValue.ToLower))
            PQ.RewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE
            Return New LuceneFilterQuery With {.FilterQuery = PQ}
        End Function

        Public Overrides Function ParseFilter(ByVal objFilter As Business.LuceneFilterQuery) As Boolean
            If TypeOf (objFilter.FilterQuery) Is PrefixQuery Then
                Dim PQ As PrefixQuery = DirectCast(objFilter.FilterQuery, PrefixQuery)
                If PQ.Prefix.Field = FieldName Then
                    FieldValue = PQ.Prefix.Text
                    If FieldValue.Length = 1 Then
                        Return True
                    End If
                End If
            End If
            Return False
        End Function

        Public Sub Lettre_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim link As LinkButton = CType(sender, LinkButton)
            link.CssClass = "NormalBold"

            FieldValue = link.CommandArgument
            RaiseEvent Click(link.CommandArgument)
            If SearchOnClick Then
                OnFilterChanged()
            End If
        End Sub

        Protected Overrides Property FieldValue() As String
            Get
                Return CStr(ViewState("FieldValue"))
            End Get
            Set(ByVal value As String)
                ViewState("FieldValue") = value
            End Set
        End Property

        Public Property DisplayOnTwoLines() As Boolean
            Get
                Return CBool(ViewState("DisplayOnTwoLines"))
            End Get
            Set(ByVal value As Boolean)
                ViewState("DisplayOnTwoLines") = value
            End Set
        End Property

        Public Property SearchOnClick() As Boolean
            Get
                Return CBool(ViewState("SearchOnClick"))
            End Get
            Set(ByVal value As Boolean)
                ViewState("SearchOnClick") = value
            End Set
        End Property

    End Class

End Namespace
